// pages/file/file.js
const app = getApp();
import {
  hexMD5
} from "../../utils/md5.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mealsList: '',

    DANCE_NAME: '', //店面名称
    DANCE_DESC: '', //商家名称
    DANCE_ADD: '', //地址
    DANCE_TEXT: '', //电话
    SHIPINHAO_URL:'',//基本介绍



    // kouhao: '', //口号
    // mingchen: '', //名称
    // miaoshu: "", //描述
    // videpath: '', //视频地址
    avatar: '', //图片地址
    avatartwo:'',//图片二
    show: false, //显示
    videshow: true, //隐藏视频
    opacity: false, // 显示图标
    opacitytwo: false, // 显示图标

    opacityimg: true, //隐藏封面
    opacityimgtwo: true, //隐藏封面



  },

  back(){
    wx.navigateBack({
      delta: 1 // 表示返回上一级，如果delta大于1则表示返回多级
      });
  },
  // filevide() {
  //   const that = this
  //   // 选择视频  
  //   wx.chooseVideo({
  //     success: function (res) {
  //       console.log(res);
  //       var tempFilePaths = res.tempFilePath
  //       console.log('tempFilePaths', tempFilePaths);
  //       console.log('大小', res.size);
  //       //限制上传图片大小（50M）    5242880
  //       if (res.size < 1024 * 1024 * 10 || res.size > 1024 * 1024 * 60) {
  //         wx.showModal({
  //           title: '请上传大于10MB小于50MB的视频!',
  //           content: '请重新上传',
  //           showCancel: false,
  //         });
  //       } else {
  //         console.log('上传的视频', tempFilePaths);
  //         wx.uploadFile({
  //           url: app.globalData.interface+'/squareDance/API_5107_uploadFileCOS ', // 
  //           filePath: tempFilePaths,
  //           name: "FIMG",


  //           header: {
  //             'Content-Type': 'multipart/form-data'
  //           },
  //           formData: {
  //             KEY: 'xianjgjKey9999',
  //             OPEN_ID: that.data.openid, //用户OPEN_ID
  //             session_key: that.data.session_key,
  //             OPEN_UUID: that.data.jiami
  //           },
  //           success: function (response) {
  //             // 上传成功  
  //             console.log('response', JSON.parse(response.data));
  //             const videfail = JSON.parse(response.data)

  //             if (videfail.result == "success" && videfail.newFile) {
  //               wx.showToast({
  //                 title: '上传成功',
  //                 icon: 'success',
  //                 duration: 1500,
  //               })
  //               console.log('44444444', videfail.newFile);

  //               setTimeout(() => {
  //                 that.setData({
  //                   videpath: videfail.newFile,
  //                   show: true, //隐藏icon
  //                   videshow: false //显示视频
  //                 })
  //               }, 500)
  //               console.log('上传后返回的地址', that.data.videpath);
  //             } else {
  //               wx.showModal({
  //                 title: '上传视频失败',
  //                 content: '请重新上传',
  //                 showCancel: false,
  //               });
  //             }

  //           },
  //           fail: function (res) {
  //             // 上传失败  
  //             wx.showModal({
  //               title: '上传视频失败',
  //               content: '请重新上传',
  //               showCancel: false,
  //             });

  //           }
  //         })

  //       }





  //       // // 将选择的视频上传到服务器  
  //       // wx.showLoading({
  //       //   title: '上传中...',
  //       //   mask: true,
  //       // })


  //     }
  //   })
  // },

  // 上传图片
  bindimg() {
    const that = this
    wx.chooseImage({
 
      sizeType: ["original", "compressed"], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ["album", "camera"], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        console.log(res);
        console.log('444', res.tempFiles[0].size);
        if (res.tempFiles[0].size > 4500000) {
          wx.showModal({
            content: '上传图片不能大于4M!',
            showCancel: false,
          });
        } else {
          console.log('tupai', res.tempFiles[0].path);
          wx.uploadFile({
            url: 'https://jiagaojie.xajiarong.net/youchuhuiqin/Test/API_1167_uploadFileCOS', // 
            method: 'POST',
            name: "FIMG",
            filePath: res.tempFiles[0].path,
            header: {
              'Content-Type': 'multipart/form-data'
            },
            formData: {
              KEY: 'xianjgjKey9999',
              OPEN_ID: that.data.openid, //用户OPEN_ID
              session_key: that.data.session_key,
              OPEN_UUID: that.data.jiami
            },
            success: function (res) {
              console.log(JSON.parse(res.data));
              let imgpath = JSON.parse(res.data)
              console.log('hahah', imgpath);
              if (imgpath.result == "success" && imgpath.newFile) {
                wx.showToast({
                  title: '上传成功',
                  icon: 'success',
                  duration: 1500,
                })
                console.log('44444444', imgpath.newFile);

                setTimeout(() => {
                  that.setData({
                    avatar: imgpath.newFile,
                    opacity: true, // 隐藏icon图标
                    // opacitytwo: true, // 隐藏icon图标
                    opacityimg: false, //显示封面
                  })
                }, 500)
                console.log('上传后返回的地址', that.data.videpath);
              } else {
                wx.showModal({
                  title: '上传图片失败',
                  content: '请重新上传',
                  showCancel: false,
                });
              }
            }
          })
        }
      }
    })
  },


  bindimgtwo() {
    const that = this
    wx.chooseImage({
 
      sizeType: ["original", "compressed"], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ["album", "camera"], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        console.log(res);
        console.log('444', res.tempFiles[0].size);
        if (res.tempFiles[0].size > 4500000) {
          wx.showModal({
            content: '上传图片不能大于4M!',
            showCancel: false,
          });
        } else {
          console.log('tupai', res.tempFiles[0].path);
          wx.uploadFile({
            url: 'https://jiagaojie.xajiarong.net/youchuhuiqin/Test/API_1167_uploadFileCOS', // 
            method: 'POST',
            name: "FIMG",
            filePath: res.tempFiles[0].path,
            header: {
              'Content-Type': 'multipart/form-data'
            },
            formData: {
              KEY: 'xianjgjKey9999',
              OPEN_ID: that.data.openid, //用户OPEN_ID
              session_key: that.data.session_key,
              OPEN_UUID: that.data.jiami
            },
            success: function (res) {
              console.log(JSON.parse(res.data));
              let imgpath = JSON.parse(res.data)
              console.log('hahah', imgpath);
              if (imgpath.result == "success" && imgpath.newFile) {
                wx.showToast({
                  title: '上传成功',
                  icon: 'success',
                  duration: 1500,
                })
                console.log('44444444', imgpath.newFile);

                setTimeout(() => {
                  that.setData({
                    avatartwo: imgpath.newFile,
                    // opacity: true, // 隐藏icon图标
                    opacitytwo: true, // 隐藏icon图标
                    opacityimgtwo: false, //显示封面
                  })
                }, 500)
                console.log('上传后返回的地址', that.data.videpath);
              } else {
                wx.showModal({
                  title: '上传图片失败',
                  content: '请重新上传',
                  showCancel: false,
                });
              }
            }
          })
        }
      }
    })
  },




















  fromnum(e) {
    let value = e.detail.value
    value = value.trim() //把空格去掉
    this.setData({
      gonghao: value
    })
  },
  fromname(e) {
    let value = e.detail.value
    value = value.trim() //把空格去掉
    this.setData({
      mingchen: value
    })
  },
  tarea(e) {
    let value = e.detail.value
    value = value.trim() //把空格去掉
    this.setData({
      miaoshu: value
    })
  },

  submit() {
    console.log('444');
    if (this.data.DANCE_NAME && this.data.DANCE_DESC && this.data.DANCE_ADD && this.data.SHIPINHAO_URL  && this.data.DANCE_TEXT && this.data.avatar&& this.data.avatartwo) {
      console.log('555', this.data.DANCE_NAME);
      wx.showLoading({
        title: '加载中...',
      })

      wx.request({
        url: app.globalData.interface+'/squareDance/API_5201_addBeautifulShanghu ',
        method: 'POST',
        data: {
          DANCE_NAME: this.data.DANCE_NAME, //舞蹈名称
          DANCE_DESC: this.data.DANCE_DESC, //舞蹈描述
          DANCE_TEXT: this.data.DANCE_TEXT, //口号
          EMPLOYEE_ID: this.data.mealsList, //员工工号
          DANCE_ADD: this.data.DANCE_ADD,
          SHIPINHAO_URL: this.data.SHIPINHAO_URL,
          DANCE_IMG: this.data.avatar, //图片地址
          SHIPINHAO_TYPE: this.data.avatartwo,
          OPEN_ID: this.data.openid, //用户OPEN_ID
          session_key: this.data.session_key,
          OPEN_UUID: this.data.jiami
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: res => {
          console.log('res', res);
          console.log('res', res.data.result);

          if (res.data.result == 'success') {
            wx.hideLoading()
            const text = res.data.data
            wx.showModal({
              content: text,
              showCancel: false,
            });

          } else {
            wx.showModal({
              content: res.data.data,
              showCancel: false,
            });
            wx.hideLoading()
          }
        },
        fail: (res => {
          wx.hideLoading()
        })
      })

    } else {
      wx.showModal({
        content: '请完善信息',
        showCancel: false,
      });
    }


  },


  reset() {
    this.setData({

      DANCE_NAME: '', //店面名称
      DANCE_DESC: '', //商家名称
      DANCE_ADD: '', //地址
      SHIPINHAO_URL:'',
      DANCE_TEXT: '', //电话
      avatar: '', //图片地址
      avatartwo:'',//图片二
      show: false, //显示
      videshow: true, //隐藏视频
      opacity: false, // 显示图标
      opacitytwo: false, // 显示图标
      opacityimg: true, //隐藏封面
      opacityimgtwo: true, //隐藏封面
    })
  },





  //验证手机号
  mobileInput(e) {
    let value = e.detail.value.replace(/\D/g, '')
    console.log(value);



    this.setData({
      mobile: value,
    })

    var myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(16[0-9]{1})|(19[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (this.data.mobile.length == 0) {
      wx.showToast({
        title: '输入的手机号为空，请重新输入！',
        icon: 'none',
        duration: 1500
      })
      this.setData({
        DANCE_TEXT: '',
        mobileFormat: false,
        xinxi: true,
      })
    } else if (this.data.mobile.length < 11) {

      wx.showToast({
        title: '手机号长度有误，请重新输入！',
        icon: 'none',
        duration: 1500
      })
      this.setData({
        DANCE_TEXT: '',
        mobileFormat: false,
      })

    } else if (!myreg.test(this.data.mobile)) {
      wx.showToast({
        title: '手机号有误，请重新输入！',
        icon: 'none',
        duration: 1500
      })
      this.setData({
        DANCE_TEXT: '',
        mobileFormat: false,
      })

    } else {
      this.setData({
        mobileFormat: true,
      })
    }
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    let that = this



    that.setData({
      openid: app.globalData.openid,
      jiami: app.globalData.jiami,
      session_key: app.globalData.session_key,

      OPEN_ID: app.globalData.openid,
    })



    if (that.data.jiami == '' || that.data.session_key == '') {



      wx.login({
        success: function (res) {
          console.log(res);
          if (res.code) { //wx.login获取code。
            console.log(res.code);
            //发起网络请求
            wx.request({
              url: app.globalData.interface+'/post/API_1062_appGetOpenid',
              method: 'GET',
              //向后端发送的数据
              data: {
                code: res.code //将code发送到后台服务器。
              },
              header: {
                "Content-Type": "application/x-www-form-urlencoded" //POST方式是这个
              },
              success: res => {
                console.log(res);
                if (res.data.result != 'success') {
                  console.log(1);
                } else {

                  that.setData({
                    openid: res.data.openid,
                    jiami: hexMD5(`${res.data.openid.substring(0,20)}${res.data.session_key.substring(0,10)}`),
                    OPEN_ID: res.data.openid,
                    session_key: res.data.session_key,
                  })
                  console.log(that.data.jiami);
                  console.log(that.data.OPEN_ID);
                  console.log(that.data.session_key);
                }
              }
            })
          } else {
            console.log('获取用户登录态失败！' + res.errMsg)
          }
        }
      });


    } else {
      console.log('成功');
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      mealsList: app.globalData.mealsList
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

})